﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Character;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

// Token: 0x020003D6 RID: 982
public class UpLoader : LoaderBase
{
	// Token: 0x0600187E RID: 6270 RVA: 0x0009FD28 File Offset: 0x0009DF28
	private void Awake()
	{
		this.upDownloaderButton.onClick.AddListener(new UnityAction(this.SwapToDownloader));
		this.checkRuleButton.onClick.AddListener(new UnityAction(this.OnCheckRule));
		this.uploadButton.onClick.AddListener(new UnityAction(this.OnUpload));
		this.handleNameInput.text = GlobalData.uploaderHandleName;
		this.handleNameInput.onEndEdit.AddListener(new UnityAction<string>(this.OnHandleName));
	}

	// Token: 0x0600187F RID: 6271 RVA: 0x0009FDB8 File Offset: 0x0009DFB8
	private void Start()
	{
		if (!GlobalData.showUploaderRule)
		{
			this.ruleRoot.gameObject.SetActive(true);
			GlobalData.showUploaderRule = true;
		}
		this.CheckServerHash();
	}

	// Token: 0x06001880 RID: 6272 RVA: 0x0009FDE4 File Offset: 0x0009DFE4
	private void Update()
	{
		Card_Data selectedCard = this.charaList.GetSelectedCard();
		this.uploadButton.interactable = (selectedCard != null && this.handleNameInput.text.Length > 0);
	}

	// Token: 0x06001881 RID: 6273 RVA: 0x0009FE24 File Offset: 0x0009E024
	private void Upload()
	{
		Card_Data selectedCard = this.charaList.GetSelectedCard();
		if (selectedCard == null || selectedCard.charaInfo == null)
		{
			return;
		}
		string text = this.handleNameInput.text;
		if (text.Length == 0)
		{
			return;
		}
		string text2 = selectedCard.charaInfo.name;
		if (text2.Length > 16)
		{
			text2 = text2.Substring(0, 16);
		}
		byte[] array = UpLoader.LoadData(selectedCard.file);
		if (array == null)
		{
			return;
		}
		string value = Convert.ToBase64String(Encoding.UTF8.GetBytes(text));
		string value2 = Convert.ToBase64String(Encoding.UTF8.GetBytes(this.commentInput.text));
		string value3 = Convert.ToBase64String(Encoding.UTF8.GetBytes(text2));
		string hashAll = UpLoader.GetHashAll(array);
		string hashParam = this.GetHashParam(array);
		GlobalData.Debug_Log("Hash all:" + hashAll + " param:" + hashParam);
		CustomParameter customParameter = new CustomParameter(this.charaList.Sex);
		customParameter.Load(selectedCard.file, true, true);
		int num = (int)(customParameter.body.GetBustSize() / 0.333f);
		num = Mathf.Clamp(num, 0, 2);
		int i = (int)((this.charaList.Sex != SEX.FEMALE) ? BackHairData.HAIR_TYPE.SHORT : CustomDataManager.GetHair_Back(customParameter.hair.parts[0].ID).type);
		int num2 = (int)(customParameter.body.GetHeight() / 0.333f);
		num2 = Mathf.Clamp(num2, 0, 2);
		int i2 = -1;
		int i3 = (this.charaList.Sex != SEX.MALE) ? 1 : 0;
		string userID = this.upDown.UserID;
		if (this.charaList.Sex == SEX.MALE)
		{
			i = 255;
			num2 = 255;
			num = 255;
		}
		if (customParameter.CheckWrongParam())
		{
			base.ShowMessage("不正なデータです\nファイルが破損しているか\n不正に改変されたデータの可能性があります\nこのデータはアップロード出来ません");
			return;
		}
		UpLoader.HASH_CHECK hash_CHECK = this.CheckUploadHash(hashAll, hashParam, ref i2);
		if (hash_CHECK != UpLoader.HASH_CHECK.OK)
		{
			base.ShowMessage("既に同じデータがアップロードされています");
			return;
		}
		WWWForm wwwform = new WWWForm();
		wwwform.AddField("mode", 5);
		wwwform.AddField("pid", i2);
		wwwform.AddField("uid", userID);
		wwwform.AddField("handlename", value);
		wwwform.AddField("products_description", value2);
		wwwform.AddField("basuto", num);
		wwwform.AddField("hair", i);
		wwwform.AddField("sex", i3);
		wwwform.AddField("shinchou", num2);
		wwwform.AddField("title", value3);
		wwwform.AddBinaryData("png", array);
		wwwform.AddField("hash_png", hashAll);
		wwwform.AddField("hash_param", hashParam);
		wwwform.AddField("state", 0);
		wwwform.AddField("seikaku", 0);
		wwwform.AddField("resistH", 0);
		wwwform.AddField("resistPain", 0);
		wwwform.AddField("resistAnal", 0);
		this.netWWW.Post("http://up.illusion.jp/phome_upload/chara/master/unity/getData.php", "アップロード中...", wwwform, delegate(WWW www)
		{
			this.Upload_Complete(www);
		}, delegate(WWW www)
		{
			this.upDown.PostError(www, "アップロードに失敗しました");
		});
	}

	// Token: 0x06001882 RID: 6274 RVA: 0x000A0154 File Offset: 0x0009E354
	private static byte[] LoadData(string file)
	{
		byte[] array = null;
		using (FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
		{
			array = new byte[fileStream.Length];
			using (BinaryReader binaryReader = new BinaryReader(fileStream))
			{
				array = binaryReader.ReadBytes(array.Length);
			}
		}
		return array;
	}

	// Token: 0x06001883 RID: 6275 RVA: 0x000A01CC File Offset: 0x0009E3CC
	private UpLoader.HASH_CHECK CheckUploadHash(string hashPng, string hashParam, ref int pid)
	{
		int i = 0;
		while (i < this.hashInfos.Count)
		{
			if (this.hashInfos[i].hashpng == hashPng)
			{
				return UpLoader.HASH_CHECK.SAME_CARD;
			}
			if (this.hashInfos[i].hashparam == hashParam)
			{
				if (this.hashInfos[i].userId == this.upDown.UserID)
				{
					pid = this.hashInfos[i].cardID;
					return UpLoader.HASH_CHECK.SAME_MY_CARD;
				}
				return UpLoader.HASH_CHECK.SAME_CARD;
			}
			else
			{
				i++;
			}
		}
		return UpLoader.HASH_CHECK.OK;
	}

	// Token: 0x06001884 RID: 6276 RVA: 0x000A0270 File Offset: 0x0009E470
	private void Upload_Complete(WWW www)
	{
		this.CheckServerHash();
		base.ShowMessage("アップロードが完了しました");
	}

	// Token: 0x06001885 RID: 6277 RVA: 0x000A0284 File Offset: 0x0009E484
	private void CheckServerHash()
	{
		WWWForm wwwform = new WWWForm();
		wwwform.AddField("mode", 0);
		this.netWWW.Post("http://up.illusion.jp/phome_upload/chara/master/unity/getData.php", "ハッシュ確認...", wwwform, delegate(WWW www)
		{
			this.CheckServerHash_Complete(www);
		}, delegate(WWW www)
		{
			this.upDown.PostError(www, "ハッシュデータの取得に失敗しました");
		});
	}

	// Token: 0x06001886 RID: 6278 RVA: 0x000A02D4 File Offset: 0x0009E4D4
	private void CheckServerHash_Complete(WWW www)
	{
		this.hashInfos.Clear();
		string[] array = www.text.Split(new char[]
		{
			'\n'
		});
		foreach (string text in array)
		{
			if (string.Empty == text)
			{
				break;
			}
			string[] array3 = text.Split(new char[]
			{
				'\t'
			});
			if (array3.Length >= 3 && !(string.Empty == array3[0]) && !(string.Empty == array3[1]) && !(string.Empty == array3[2]))
			{
				int cardID = int.Parse(array3[0]);
				this.hashInfos.Add(new ServerHashInfo(cardID, array3[1], array3[2], array3[3]));
			}
		}
	}

	// Token: 0x06001887 RID: 6279 RVA: 0x000A03B8 File Offset: 0x0009E5B8
	public static string GetHashAll(byte[] data)
	{
		byte[] hashBytes = UpLoader.CreateSha256(data, "CreateHash");
		return UpLoader.HashBytesToString(hashBytes);
	}

	// Token: 0x06001888 RID: 6280 RVA: 0x000A03D8 File Offset: 0x0009E5D8
	public string GetHashParam(byte[] data)
	{
		int num = (int)PNG_Loader.CheckSize(data);
		int num2 = data.Length - num;
		byte[] array = new byte[num2];
		Buffer.BlockCopy(data, num, array, 0, num2);
		byte[] hashBytes = UpLoader.CreateSha256(array, "CreateHash");
		return UpLoader.HashBytesToString(hashBytes);
	}

	// Token: 0x06001889 RID: 6281 RVA: 0x000A0418 File Offset: 0x0009E618
	private static byte[] CreateSha256(byte[] data, string key)
	{
		byte[] bytes = Encoding.UTF8.GetBytes(key);
		HMACSHA256 hmacsha = new HMACSHA256(bytes);
		return hmacsha.ComputeHash(data);
	}

	// Token: 0x0600188A RID: 6282 RVA: 0x000A0440 File Offset: 0x0009E640
	private static string HashBytesToString(byte[] hashBytes)
	{
		if (hashBytes == null)
		{
			return string.Empty;
		}
		StringBuilder stringBuilder = new StringBuilder();
		foreach (byte b in hashBytes)
		{
			stringBuilder.Append(b.ToString("x2"));
		}
		return stringBuilder.ToString();
	}

	// Token: 0x0600188B RID: 6283 RVA: 0x000A0494 File Offset: 0x0009E694
	private void SwapToDownloader()
	{
		SystemSE.Play(SystemSE.SE.CHOICE);
		this.upDown.ChangeMode(UpDown.MODE.DOWNLOAD);
	}

	// Token: 0x0600188C RID: 6284 RVA: 0x000A04A8 File Offset: 0x0009E6A8
	private void OnCheckRule()
	{
		SystemSE.Play(SystemSE.SE.YES);
		this.ruleRoot.gameObject.SetActive(true);
	}

	// Token: 0x0600188D RID: 6285 RVA: 0x000A04C4 File Offset: 0x0009E6C4
	private void OnUpload()
	{
		SystemSE.Play(SystemSE.SE.YES);
		string text = "選択中のカードをアップロードします\nよろしいですか？";
		this.upDown.GC.CreateModalYesNoUI(text, new Action(this.Upload), null);
	}

	// Token: 0x0600188E RID: 6286 RVA: 0x000A04FC File Offset: 0x0009E6FC
	private void OnHandleName(string str)
	{
		GlobalData.uploaderHandleName = str;
	}

	// Token: 0x04001BF6 RID: 7158
	private const string MSG_Uploading = "アップロード中...";

	// Token: 0x04001BF7 RID: 7159
	private const string MSG_CheckHash = "ハッシュ確認...";

	// Token: 0x04001BF8 RID: 7160
	[SerializeField]
	private Button upDownloaderButton;

	// Token: 0x04001BF9 RID: 7161
	[SerializeField]
	private Button checkRuleButton;

	// Token: 0x04001BFA RID: 7162
	[SerializeField]
	private GameObject ruleRoot;

	// Token: 0x04001BFB RID: 7163
	[SerializeField]
	private Button uploadButton;

	// Token: 0x04001BFC RID: 7164
	[SerializeField]
	private InputField handleNameInput;

	// Token: 0x04001BFD RID: 7165
	[SerializeField]
	private InputField commentInput;

	// Token: 0x04001BFE RID: 7166
	[SerializeField]
	private UploaderCardFileList charaList;

	// Token: 0x04001BFF RID: 7167
	[SerializeField]
	private string def_handleName = "名も無き紳士";

	// Token: 0x04001C00 RID: 7168
	private List<ServerHashInfo> hashInfos = new List<ServerHashInfo>();

	// Token: 0x020003D7 RID: 983
	private enum HASH_CHECK
	{
		// Token: 0x04001C02 RID: 7170
		OK,
		// Token: 0x04001C03 RID: 7171
		SAME_CARD,
		// Token: 0x04001C04 RID: 7172
		SAME_MY_CARD
	}
}
